'''
Created on 2013-6-7

@author: Administrator
'''

class Temperature(object):
    '''
    classdocs
    '''
    coefficients = {'c':(1.0, 0.0, -273.15), 'f':(1.8, -273.15, 32.0), 'r':(1.8, 0.0, 0.0)}

    def __init__(self, ** kwargs):
        '''
        Constructor
        '''
        try:
            name , value = kwargs.popitem()
        except KeyError:
            name, value= 'k', 0
        
        if kwargs or name not in 'kcfr':
            kwargs[name] = value
            raise TypeError, 'invalid arguments %r' % kwargs
        setattr(self, name, value)
    
    def __getattr__(self, name):
        try:
            eq = self.coefficients[name]
        except KeyError:
            raise AttributeError, name
        return (self.k + eq[1]) * eq[0] + eq[2]
    
    def __setattr__(self, name, value):
        if name in self.coefficients:
            eq = self.coefficients[name]
            self.k = (value - eq[2]) / eq[0] - eq[1]
        elif name == 'k':
            #委托给父类进行设置
            object.__setattr__(self, name, value)
        else:
            raise AttributeError, name
    def __str__(self, *args, **kwargs):
        return "%s K" % self.k
    def __repr__(self, *args, **kwargs):
        return "Temperature(k=%r)" % self.k
        
class MyClass(object):
    def __init__(self, name, value):
        print 'init MyClass'
        self.name = name
        self.value = value
    def __setattr__(self, name, value):
         print '%s: %s' % (name, value)
         object.__setattr__(self, name, value)
myClass = MyClass('hanjiewu', '860728')
